package action.util;
import java.util.ArrayList;
import java.util.List;
import action.pojo.FtpConnect;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class FtpConnectSQLITE {
	private SQLiteDatabase database;
	private DatabaseHelper helper;
	private String[] allColumns = {DatabaseHelper.COLUMN_ID_FTP,DatabaseHelper.COLUMN_HOSTNAME,DatabaseHelper.COLUMN_PORT,
			DatabaseHelper.COLUMN_USERNAME,DatabaseHelper.COLUMN_PASSWORD};

	private String[] allColummsFavorite = {DatabaseHelper.COLUMN_ID_FAVORITE_FTP,DatabaseHelper.COLUMN_NAME_FAVORITE_FTP,
			DatabaseHelper.COLUMN_PATH_FAVORITE_FTP,DatabaseHelper.COLUMN_FOLDER_FAVORITE_FTP,
			DatabaseHelper.COLUMN_HOSTNAME_FAVORITE,DatabaseHelper.COLUMN_PORT_FAVORITE,DatabaseHelper.COLUMN_USERNAME_FAVORITE_FTP,
			DatabaseHelper.COLUMN_PASSWORD_FAVORITE_FTP};
	
	private FtpConnect ftpConnect ;

	public FtpConnectSQLITE(Context context){
		helper = new DatabaseHelper(context);
	}

	public void open() throws SQLException {
		database = helper.getWritableDatabase();
	}

	public void close() {
		helper.close();
	}
	
// begin create ftp server part

	public FtpConnect createFtpConnect(String host,int port,String us,String ps) {
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.COLUMN_HOSTNAME, host);
		values.put(DatabaseHelper.COLUMN_PORT, port);
		values.put(DatabaseHelper.COLUMN_USERNAME, us);
		values.put(DatabaseHelper.COLUMN_PASSWORD, ps);

		long insertId = database.insert(DatabaseHelper.TABLE_FTP, null,
				values);
		Cursor cursor = database.query(DatabaseHelper.TABLE_FTP,
				allColumns, DatabaseHelper.COLUMN_ID_FTP + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		FtpConnect newFtpConnect = cursorToFtp(cursor);
		cursor.close();
		return newFtpConnect;
	}

	public List<FtpConnect> getAllConnect() {
		List<FtpConnect> ftpConnects = new ArrayList<FtpConnect>();

		Cursor cursor = database.query(DatabaseHelper.TABLE_FTP,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			ftpConnect = cursorToFtp(cursor);
			ftpConnects.add(ftpConnect);

			cursor.moveToNext();
		}
		cursor.close();
		return ftpConnects;
	}

	public void deleteFtp(FtpConnect ftpConnect) {
		long id = ftpConnect.getIdftpconnect();
		database.delete(DatabaseHelper.TABLE_FTP, DatabaseHelper.COLUMN_ID_FTP
				+ " = " + id, null);
		close();
	}

	// update ftp
	public boolean updateFtp(FtpConnect connect){
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.COLUMN_HOSTNAME, connect.getHostname());
		values.put(DatabaseHelper.COLUMN_PORT, connect.getPort());
		values.put(DatabaseHelper.COLUMN_USERNAME, connect.getUsername());
		values.put(DatabaseHelper.COLUMN_PASSWORD, connect.getPassword());

		return database.update(DatabaseHelper.TABLE_FTP, values, DatabaseHelper.COLUMN_ID_FTP + "=" + connect.getIdftpconnect(), null) >0;
	}

	public FtpConnect cursorToFtp(Cursor cursor){
		ftpConnect = new FtpConnect();
		try{
			ftpConnect.setIdftpconnect(cursor.getInt(0));
			ftpConnect.setHostname(cursor.getString(1));
			ftpConnect.setPort(cursor.getInt(2));
			ftpConnect.setUsername(cursor.getString(3));
			ftpConnect.setPassword(cursor.getString(4));
		}catch(Exception e){
			e.printStackTrace();
		}
		return ftpConnect;
	}
// end create ftp server part
	
	//TODO  begin favorite ftp part
	public FtpConnect createNewFavoriteFTP(FtpConnect file) {
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.COLUMN_NAME_FAVORITE_FTP, file.getFtpFile().getName());
		
		if(file.getFtpFile().isDirectory()){
			values.put(DatabaseHelper.COLUMN_FOLDER_FAVORITE_FTP,0);	
		}else{
			values.put(DatabaseHelper.COLUMN_FOLDER_FAVORITE_FTP,1);
		}
		
		values.put(DatabaseHelper.COLUMN_PATH_FAVORITE_FTP, Tool.getFTPPath(Tool.ftp_path));
		values.put(DatabaseHelper.COLUMN_HOSTNAME_FAVORITE,file.getHostname());
		values.put(DatabaseHelper.COLUMN_PORT_FAVORITE, file.getPort());
		values.put(DatabaseHelper.COLUMN_USERNAME_FAVORITE_FTP, file.getUsername());
		values.put(DatabaseHelper.COLUMN_PASSWORD_FAVORITE_FTP, file.getPassword());
		
		long insertId = database.insert(DatabaseHelper.TABLE_FAVORITE_FTP, null,
				values);
		Cursor cursor = database.query(DatabaseHelper.TABLE_FAVORITE_FTP,
				allColummsFavorite, DatabaseHelper.COLUMN_ID_FAVORITE_FTP + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		FtpConnect newFtpConnect = cursorToFavorite(cursor);
		cursor.close();
		return newFtpConnect;
	}
	
	public List<FtpConnect> getAllConnectFavorite(String hostname) {
		List<FtpConnect> ftpConnects = new ArrayList<FtpConnect>();

		Cursor cursor = database.query(DatabaseHelper.TABLE_FAVORITE_FTP,
				allColummsFavorite, DatabaseHelper.COLUMN_HOSTNAME_FAVORITE + " = " + "'"+hostname+"'", null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			ftpConnect = cursorToFavorite(cursor);
			ftpConnects.add(ftpConnect);
			
			cursor.moveToNext();
		}
		cursor.close();
		return ftpConnects;
	}
	
	public void deleteFavorite() {
		database.delete(DatabaseHelper.TABLE_FAVORITE_FTP,null, null);
		close();
	}
	
	public FtpConnect cursorToFavorite(Cursor cursor){
		 ftpConnect = new FtpConnect();
		try{
			ftpConnect.setIdfavorite(cursor.getInt(0));
			ftpConnect.setName(cursor.getString(1));
			ftpConnect.setPath(cursor.getString(2));
			ftpConnect.setIsFolder(cursor.getInt(3));
			ftpConnect.setHostname(cursor.getString(4));
			ftpConnect.setPort(cursor.getInt(5));
			ftpConnect.setUsername(cursor.getString(6));
			ftpConnect.setPassword(cursor.getString(7));
		}catch(Exception e){
			e.printStackTrace();
		}
		return ftpConnect;
	}
	// end favorite ftp part
	
}
